package ace.j2se.l2;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author Administrator
 * 
 */
public class TxtReader {
	// public static void main(String[] args) throws IOException {
	// StringBuffer sb = new StringBuffer("");
	// //FileOutputStream ostream = new FileOutputStream("");
	// try {
	// FileReader reader = new FileReader("E:\\xh-2.txt");
	// BufferedReader br = new BufferedReader(reader);
	// String s = null;
	// while ((s = br.readLine()) != null) {
	// //if ("qq".indexOf(s) != -1) {
	// sb.append(s + '\n');
	// //}
	// }
	// br.close();
	// reader.close();
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// System.out.println(sb.toString());
	// }
	public static String fff = "e:\\xh-2.txt";

	public static void main(String[] args) throws FileNotFoundException,
			IOException {
		readSmaillFile();
	}

	public static void readSmaillFile() {
		StringBuffer sb = new StringBuffer("");
		// FileOutputStream ostream = new FileOutputStream("");
		try {
			FileReader reader = new FileReader("E:\\x-3.txt");
			BufferedReader br = new BufferedReader(reader);
			String s = null;
			while ((s = br.readLine()) != null) {
				if (s.indexOf("@qq") != -1) {
					sb.append(s + '\n');
				}
			}
			br.close();
			reader.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(sb.toString());
	}

	public static void readBigFile() throws FileNotFoundException, IOException {
		final int BUFFER_SIZE = 0x30000;// ������3M
		File f = new File(fff);
		MappedByteBuffer inputBuffer = new RandomAccessFile(f, "r")
				.getChannel().map(FileChannel.MapMode.READ_ONLY,
						f.length() / 2, f.length() / 2);

		byte[] dst = new byte[BUFFER_SIZE];// ÿ�ζ���3M������

		long start = System.currentTimeMillis();

		for (int offset = 0; offset < inputBuffer.capacity(); offset += BUFFER_SIZE) {

			if (inputBuffer.capacity() - offset >= BUFFER_SIZE) {

				for (int i = 0; i < BUFFER_SIZE; i++)

					dst[i] = inputBuffer.get(offset + i);

			} else {

				for (int i = 0; i < inputBuffer.capacity() - offset; i++)

					dst[i] = inputBuffer.get(offset + i);

			}

			int length = (inputBuffer.capacity() % BUFFER_SIZE == 0) ? BUFFER_SIZE
					: inputBuffer.capacity() % BUFFER_SIZE;

			String result = new String(dst, 0, length);
			if (result.indexOf("qq") > -1) {
				System.out.println(result);// new
			}

			// String(dst,0,length)�������ȡ�����汣����ַ����Զ�����в���

		}

		long end = System.currentTimeMillis();

		System.out.println("执行时间" + (end - start) + "");
	}
}
